package com.generator.statics.inboundStatics.service.impl;

import com.generator.statics.inboundStatics.dao.InboundStaticsMap;
import com.generator.statics.inboundStatics.pojo.*;
import com.generator.statics.inboundStatics.service.IInboundStaticsService;
import com.generator.util.PageModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class InboundStaticsService implements IInboundStaticsService {

    @Autowired
    private InboundStaticsMap inboundStaticsMap;

    public InboundStaticsMap getJoinCount() {
        return inboundStaticsMap;
    }

    public void setJoinCount(InboundStaticsMap joinCount) {
        this.inboundStaticsMap = joinCount;
    }

//    @Override
//    public List<Warehouse> getWarehouse(Warehouse warehouse) {
//        return joinCountMapper.getWarehouse(warehouse);
//    }

    @Override
    //    查询所有入库记录
    public PageModel<QueryWarehouseList> QueryWarehouseList(QueryWarehouseList queryWarehouseList, PageModel<QueryWarehouseList> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?1:pageModel.getPageSize());

        Page<Object> pageinfo = PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<QueryWarehouseList> warehouseList = inboundStaticsMap.QueryWarehouseList(queryWarehouseList);
        for (QueryWarehouseList q : warehouseList) {
            float tprice=0;
            JoinCountDetail flag = new JoinCountDetail();
            flag.setWarehousename(q.getWarehousename());
            List<JoinCountDetail> list = inboundStaticsMap.queryRkByWareID(flag);
            HashMap<String, JoinCountDetail> hp = new HashMap<String, JoinCountDetail>();
            Set<String> purid = null;
            for (JoinCountDetail jd : list) {
                purid = hp.keySet();
                if (!purid.contains(jd.getPurchaseid())){
                    hp.put(jd.getPurchaseid(),jd);
                }
                else{
                    JoinCountDetail countDetail = hp.get(jd.getPurchaseid());
                    countDetail.setTotalprice(jd.getTotalprice()+countDetail.getTotalprice());
                    countDetail.setGoodscomment(jd.getGoodscomment()+countDetail.getGoodscomment());
                    hp.put(jd.getPurchaseid(),countDetail);
                }
            }
            Iterator<String> iterator = purid.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                JoinCountDetail joinCountDetail = hp.get(next);
                tprice += joinCountDetail.getTotalprice();

            }
            q.setRknum(hp.size());
            q.setTotalprice(tprice);
        }
        PageInfo<QueryWarehouseList> warehousePageInfo = new PageInfo<QueryWarehouseList>(warehouseList);

        pageModel.setData(new ArrayList<QueryWarehouseList>(warehouseList));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());
        return pageModel;
    }

    //通过仓库拿到采购单页面
    @Override
    public PageModel<JoinCountDetail> queryRkByWareID(JoinCountDetail joinCountDetail, PageModel<JoinCountDetail> pageModel) {

        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?1:pageModel.getPageSize());

        Page<Object> pageinfo = PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());

        List<JoinCountDetail> list = inboundStaticsMap.queryRkByWareID(joinCountDetail);
        HashMap<String, JoinCountDetail> hp = new HashMap<>();
        Set<String> purid = null;

        for (JoinCountDetail countDetail: list){
            purid = hp.keySet();
            if (!purid.contains(countDetail.getPurchaseid())){
                hp.put(countDetail.getPurchaseid(),countDetail);
            }
            else {
                Integer totalcomment;
                Float totalpri;
                JoinCountDetail countDetail1 = hp.get(countDetail.getPurchaseid());
                totalcomment = countDetail.getGoodscomment()+countDetail1.getGoodscomment();
                totalpri = countDetail.getTotalprice()+countDetail1.getTotalprice();
                countDetail1.setTotalprice(totalpri);
                countDetail1.setGoodscomment(totalcomment);
                hp.put(countDetail.getPurchaseid(),countDetail1);
            }

        }
        list.clear();
        Iterator<String> iterator = purid.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            JoinCountDetail countDetail = hp.get(next);
            list.add(countDetail);
        }

        PageInfo<JoinCountDetail> warehousePageInfo = new PageInfo<JoinCountDetail>(list);

        pageModel.setData(new ArrayList<JoinCountDetail>(list));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());

        return pageModel;
    }

    //获取省份
    @Override
    public List<Province> getAllProvince() {
        return inboundStaticsMap.getAllProvince();
    }

    //获取城市
    @Override
    public List<City> getAllCity(City city) {
        return inboundStaticsMap.getAllCity(city);
    }

    //通过采购编号拿到采购明细
    @Override
    public CPurchaseDetail getCDetailsByPurchaseId(String purchaseid) {
        CPurchaseDetail cpd = inboundStaticsMap.getCDetailsByPurchaseId(purchaseid);
        String cgstate = cpd.getCgstate();
        String str1 = "";
        if (cgstate.equals("0")){
            str1 += "未入库";
        }else if (cgstate.equals("1")){
            str1 += "入库";
        }
        cpd.setCgstate(str1);

        String checkstatus = cpd.getCheckstatus();
        String str2 = "";
        if (checkstatus.equals("1")){
            str2 += "未审核";
        }
        else if(checkstatus.equals("2")){
            str2 += "审核中";
        }
        else if(checkstatus.equals("3")){
            str2 += "审核通过";
        }
        else if(checkstatus.equals("4")){
            str2 += "审核未通过";
        }
        cpd.setCheckstatus(str2);
        return cpd;
    }

    @Override
    public List<CDetails> getGoodsByPurchaseId(String purchaseid) {
        return  inboundStaticsMap.getGoodsByPurchaseId(purchaseid);
    }

    @Override
    public PageModel<QueryWarehouseList> queryWareHouseByCondition(WareHouseCondition wareHouseCondition,PageModel<QueryWarehouseList> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?1:pageModel.getPageSize());

        Page<Object> pageinfo = PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<QueryWarehouseList> warehouseList = inboundStaticsMap.queryWareHouseByCondition(wareHouseCondition);
        for (QueryWarehouseList q : warehouseList) {
            float tprice=0;
            JoinCountDetail flag = new JoinCountDetail();
            flag.setWarehousename(q.getWarehousename());
            List<JoinCountDetail> list = inboundStaticsMap.queryRkByWareID(flag);
            HashMap<String, JoinCountDetail> hp = new HashMap<String, JoinCountDetail>();
            Set<String> purid = null;
            for (JoinCountDetail jd : list) {
                purid = hp.keySet();
                if (!purid.contains(jd.getPurchaseid())){
                    hp.put(jd.getPurchaseid(),jd);
                }
                else{
                    JoinCountDetail countDetail = hp.get(jd.getPurchaseid());
                    countDetail.setTotalprice(jd.getTotalprice()+countDetail.getTotalprice());
                    countDetail.setGoodscomment(jd.getGoodscomment()+countDetail.getGoodscomment());
                    hp.put(jd.getPurchaseid(),countDetail);
                }
            }
            Iterator<String> iterator = purid.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                JoinCountDetail joinCountDetail = hp.get(next);
                tprice += joinCountDetail.getTotalprice();

            }
            q.setRknum(hp.size());
            q.setTotalprice(tprice);
        }
        PageInfo<QueryWarehouseList> warehousePageInfo = new PageInfo<QueryWarehouseList>(warehouseList);

        pageModel.setData(new ArrayList<QueryWarehouseList>(warehouseList));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());
        return pageModel;
    }


}
